// 02local/05reserveSeat/reserveSeat.js
var co = require('../../resource/js/common.js')
var app = getApp()
var _this

Page({

    /* 页面的初始数据*/
    data: {
        //当前日期
        nowDate: '',
        // 结束日期
        endDate: '',
        //当前时间
        nowTime: '',
        //结束时间
        endTime: '',
        date: '点击选择日期',
        time: '点击选择时间',
        // 判断
        isDate: false,
        isTime: false,
        dinersNumber: false,
        userName: false,
        phoneNumber: false,
        isPhoneNumber: false,
        // 备注信息
        text: '',
        // 商家ID
        Biz_ID: '',
        // 用餐人数
        peopleNumber: null,
        // 点菜'选桌
        id: 1,
        idx: 1,
        //选桌按钮
        selectTable: '手动选桌',
        table_ID: '',
        Order_ID: '',
    },

    /* 生命周期函数--监听页面加载*/
    onLoad: function (options) {
        _this = this
        // 商店ID
        var Biz_ID = app.Data.shopInfo.Biz_ID
        //获取时间当前时间日期
        var date = new Date()
        var y = date.getFullYear()
        var m = date.getMonth()
        var d = date.getDate()
        var h = date.getHours()
        var ms = date.getMinutes()
        var nowDate = y + '-' + m + '-' + d            //当前日期
        var nowTime = h + ':' + ms                     //当前时间
        //设置半年内预定
        if (m + 6 >= 12) {
            m = (m + 6) - 12
            y = y + 1
        } else {
            m = m + 6
            d = d
        }
        var endDate = y + '-' + m + '-' + d            //结束日期
        this.setData({
            nowDate: nowDate,
            endDate: endDate,
            nowTime: nowTime,
            Biz_ID: Biz_ID
        })
    },
    //日期选择
    bindDateChange: function (e) {
        //   console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            date: e.detail.value,
            isDate: false
        })
    },
    //时间选择
    bindTimeChange: function (e) {
        //   console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            time: e.detail.value,
            isTime: false
        })
    },
    // 备注信息
    bindText: function (e) {
        this.setData({ text: e.detail.value })
    },
    // 提交预定信息
    sumbitReserveSeat: function (e) {
        // console.log("【获取form数据】===", e.detail.value, )
        var pattern = /^((1[3,5,8][0-9])|(14[5,7])|(17[0,6,7,8])|(19[7]))\d{8}$/
        var DN = e.detail.value.dinersNumber            //用餐人数
        var UN = e.detail.value.userName                //联系人姓名
        var PN = e.detail.value.phoneNumber             //联系人电话
        var date = this.data.date                       //选择的日期
        var time = this.data.time                       //选择的时间
        var text = this.data.text                       //备注信息
        if (date == '点击选择日期') {
            this.setData({ isDate: true })
        }
        if (time == '点击选择时间') {
            this.setData({ isTime: true })
        }
        if (DN == '') {
            this.setData({ dinersNumber: true })
        }
        if (UN == '') {
            this.setData({ userName: true })
        }
        if (PN == '') {
            this.setData({ phoneNumber: true })
        } else if (!pattern.test(PN)) {
            this.setData({ isPhoneNumber: true })
        }
        if (date != '点击选择日期' && DN != '' && UN != '' && PN != '' && time != '点击选择时间') {
            var param = {
                Arrival_Time: this.data.date + " " + this.data.time, //到店时间
                Guest_Num: DN,                               //用餐人数
                Contacts: UN,                                //联系人
                Contacts_Phone: PN,                          //联系人手机号
                User_ID: app.Data.userInfo.User_ID,          //预定人ID
                unionid: app.Data.userInfo.unionid,          //预订人微信联盟ID
                Biz_ID: this.data.Biz_ID,                    //店家ID
                Select_Table_ID: this.data.table_ID,         //预定座位
                Order_ID: this.data.Order_ID,                //预定点菜
                // Pay_Status:,                              //付款状态
                Remark: text,                                //备注信息        
            }
            // console.log("【输出param】===",param)
            co.getMealApi('seat/Smg/bookSeats', param, function (res) {
                // console.log("【获取返回信息】===",res.data)
                if (res.data = 0) {
                    wx: wx.switchTab({
                        url: '/03myCenter/01myCenter/myCenter',
                    })
                } else {

                }
            })
        }
    },
    // 隐藏提示错误信息
    hideErroInfo: function () {
        this.setData({
            phoneNumber: false,
            userName: false,
            dinersNumber: false,
            isPhoneNumber: false
        })
    },
    // 获取用餐人数
    People: function (e) {
        this.setData({
            peopleNumber: e.detail.value
        })
    },
    // 判断是否输入用餐人数
    changeSeat: function () {
        var n = this.data.peopleNumber
        if (n == '') {
            this.setData({
                dinersNumber: true
            })
        }
    },
    // 系统匹配桌和到店点菜
    selectTable: function () {
        this.getTableList(this.data.Biz_ID, this.data.peopleNumber)
        this.setData({
            Order_ID: 0
        })
    },
    // 手动选桌
    seatActive: function (e) {
        var idx = e.currentTarget.id
        var h
        if (idx == '0') {
            if (this.data.peopleNumber != null) {
                wx.navigateTo({
                    url: '/02local/06selectDesk/selectDesk?Biz_ID=' + this.data.Biz_ID + '&Guest_Num=' + this.data.peopleNumber + '&value=' + '预约',
                })
            } else {
                h = true
            }
        } else if (idx == '1') {
            this.getTableList(this.data.Biz_ID, this.data.peopleNumber)
            this.setData({
                selectTable: '手动选桌'
            })
            h = false
        }
        this.setData({
            idx: idx,
            dinersNumber: h
        })
    },
    //选桌完获取桌信息
    onShow: function () {
        if (!co.empty(app.Data.tableInfo)) {
            this.setData({
                table_ID: app.Data.Table_ID,
                selectTable: '已选' + app.Data.tableInfo + '(更换)'
            })
        }
        this.setData({
            Order_ID: app.Data.Order_ID
        })
    },
    // 现在点菜和到店点菜
    orderActive: function (e) {
        var id = e.currentTarget.id
        var h
        if (id == '0') {
            if (this.data.peopleNumber != null) {
                wx.navigateTo({
                    url: '/02local/03foodInfo/foodInfo?Biz_ID=' + this.data.Biz_ID + '&value=' + '预约' + '&Table_ID=' + this.data.table_ID + '&Guest_Num=' + this.data.peopleNumber,
                })
            } else {
                h = true
            }
        } else if (id == '1') {
            h = false
            this.setData({
                Order_ID: 0
            })
        }
        this.setData({
            id: id,
            dinersNumber: h
        })
    },
    // 获取餐桌信息
    getTableList: function (biz_id, num) {
        var param = {
            // this.data.Biz_ID
            Biz_ID: 75,
            Guest_Num: num
        }
        co.getMealApi('table/Tmg/selectTheBestTable', param, function (res) {
            if (res.code == 0) {
                _this.setData({
                    table_ID: res.data[0].Table_ID
                })
            }
            // console.log('【返回餐桌信息】===', _this.data.table_ID)
        })
    },
})